//
//  UIAlertController+Extensions.m
//  JQKit
//
//  Created by JQZ on 2017/12/29.
//  Copyright © 2017年 JQZ. All rights reserved.
//

#import "UIAlertController+Extensions.h"
#import "ShortcutMacros.h"
#import <objc/runtime.h>

@implementation UIAlertAction (Extensions)

char * const kJQUIAlertActionTextColor = "JQUIAlertActionTextColor";

- (UIColor *)textColor
{
    return objc_getAssociatedObject(self, kJQUIAlertActionTextColor);
}

- (void)setTextColor:(UIColor *)textColor
{
    objc_setAssociatedObject(self, kJQUIAlertActionTextColor, textColor, OBJC_ASSOCIATION_RETAIN);
    
    unsigned int count = 0;
    Ivar *ivars = class_copyIvarList([UIAlertAction class], &count);
    for(int i = 0; i < count; i++){
        Ivar ivar = ivars[i];
        NSString *ivarName = [NSString stringWithCString:ivar_getName(ivar) encoding:NSUTF8StringEncoding];
        if ([ivarName isEqualToString:@"_titleTextColor"]) {
            [self setValue:textColor forKey:@"titleTextColor"];
        }
    }
}

@end

@implementation UIAlertController (Extensions)

char * const kJQUIAlertControllerTintColor = "JQUIAlertControllerTintColor";
char * const kJQUIAlertControllerTitleColor = "JQUIAlertControllerTitleColor";
char * const kJQUIAlertControllerMessageColor = "JQUIAlertControllerMessageColor";

+ (void)load
{
    ExchangeMethod(@selector(viewDidLoad), @selector(__tt_viewDidLoad));
}

- (void)__tt_viewDidLoad
{
    [self __tt_viewDidLoad];
    
    unsigned int count = 0;
    Ivar *ivars = class_copyIvarList([UIAlertController class], &count);
    for (int i = 0; i < count; i++){
        Ivar ivar = ivars[i];
        NSString *ivarName = [NSString stringWithCString:ivar_getName(ivar) encoding:NSUTF8StringEncoding];
        
        // 标题颜色
        if ([ivarName isEqualToString:@"_attributedTitle"] && self.title && self.titleColor) {
            
            NSDictionary *attributes = @{
                                         NSForegroundColorAttributeName:self.titleColor,
                                         NSFontAttributeName:[UIFont boldSystemFontOfSize:18.0]
                                         };
            
            NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:self.title attributes:attributes];
            [self setValue:attr forKey:@"attributedTitle"];
        }
        
        // 描述颜色
        if ([ivarName isEqualToString:@"_attributedMessage"] && self.message && self.messageColor) {
            
            NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
            [paragraphStyle setLineSpacing:2.0f]; // 调整行间距
            [paragraphStyle setAlignment:NSTextAlignmentCenter];
            
            NSDictionary *attributes = @{
                                         NSForegroundColorAttributeName:self.messageColor,
                                         NSParagraphStyleAttributeName:paragraphStyle,
                                         NSFontAttributeName:[UIFont systemFontOfSize:13.0f]
                                         };
            
            NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:self.message attributes:attributes];
            [self setValue:attr forKey:@"attributedMessage"];
        }
    }
    
    // 按钮统一颜色
    if (self.tintColor) {
        for (UIAlertAction *action in self.actions) {
            if (!action.textColor) {
                action.textColor = self.tintColor;
            }
        }
    }
}

- (UIColor *)tintColor
{
    return objc_getAssociatedObject(self, kJQUIAlertControllerTintColor);
}

- (void)setTintColor:(UIColor *)tintColor
{
    objc_setAssociatedObject(self, kJQUIAlertControllerTintColor, tintColor, OBJC_ASSOCIATION_RETAIN);
}

- (UIColor *)titleColor
{
    return objc_getAssociatedObject(self, kJQUIAlertControllerTitleColor);
}

- (void)setTitleColor:(UIColor *)titleColor
{
    objc_setAssociatedObject(self, kJQUIAlertControllerTitleColor, titleColor, OBJC_ASSOCIATION_RETAIN);
}

- (UIColor *)messageColor
{
    return objc_getAssociatedObject(self, kJQUIAlertControllerMessageColor);
}

- (void)setMessageColor:(UIColor *)messageColor
{
    objc_setAssociatedObject(self, kJQUIAlertControllerMessageColor, messageColor, OBJC_ASSOCIATION_RETAIN);
}

@end
